package baralho.carta;
import java.io.*;

public class Carta implements Serializable, Comparable<Carta> {

	private Naipe naipe;
	private Valor valor;

	public Carta(Naipe naipe, Valor valor){
		//assert(naipe != null && valor != null);
		this.naipe = naipe;
		this.valor = valor;
	}
	
	public Carta(String string) {
		string = string.substring(0, string.indexOf('-'));
		String naipe = string.substring(0, string.indexOf(' '));
		String valor = string.substring(string.indexOf(' ')+1, string.length()-1);
		this.naipe = Naipe.valueOf(naipe);
		this.valor = Valor.valorDeString(valor);
	}
	
	public Naipe getNaipe(){
		return this.naipe;
	}

	public Valor getValor(){
		return this.valor;
	}

	public boolean equals(Carta c){
		assert (c != null);
		return (c.getNaipe() == this.naipe && c.getValor() == this.valor);
	}

	public boolean menorQueNaipe(Carta c) throws NullPointerException{
		return (c.getNaipe().ordinal() > this.getNaipe().ordinal()
		|| this.menorQue(c));
	}
	
	public boolean menorQue(Carta c) throws NullPointerException{
		return (c.getNaipe().ordinal() == this.getNaipe().ordinal()
		&& c.getValor().ordinal() > this.getValor().ordinal());
	}
	
	@Override
	public String toString(){
		return this.getNaipe().toString() + " " + this.getValor().toString();
	}
	
	
	public String toStringProlog(){
		String string = this.getNaipe() + "-" + (this.getValor().ordinal()+2);
		return string.toLowerCase();
	}
	
	
	public int compareTo(Carta carta) throws ClassCastException{
		assert(carta != null);
		int peso_objecto = 13 * carta.getNaipe().ordinal() + carta.getValor().ordinal();
		int peso_this = 13 * this.getNaipe().ordinal() + this.getValor().ordinal();
		
		if(peso_this > peso_objecto)
			return 1;
		else if(peso_this == peso_objecto)
			return 0;
		else
			return -1;	
	}
}
